<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item>
        <el-button plain type="primary" @click="updateForm('create')" v-hasPermi="['system:project:group:create']">
          <Icon class="mr-5px" icon="ep:plus" />
          新增用户组
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :show-overflow-tooltip="true"
      @expand-change="onExpand"
      :expand-row-keys="expandKeys"
      row-key="id"
    >
      <el-table-column type="expand">
        <template #default="props">
          <div m="4">
            <el-button
              type="primary"
              plain
              @click="addChildForm('create', props.row)"
              class="child-btn"
              v-hasPermi="['system:project:group:member:create']"
            >
              新增用户组成员
            </el-button>
            <el-table :data="props.row.children" :border="childBorder" v-if="props.row.children">
              <el-table-column align="center" label="Name" prop="name" />
              <el-table-column align="center" label="Username" prop="username" />
              <el-table-column align="center" label="accessLevel" prop="accessLevel" />
              <!-- <el-table-column align="center" label="Email" prop="email" /> -->
              <el-table-column :width="300" align="center" label="操作">
                <template #default="scope">
                  <el-button
                    link
                    type="danger"
                    @click="handleDeleteChildren(scope.row.id, props.row)"
                    v-hasPermi="['system:project:group:member:delete']"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户组" prop="fullName" />
      <el-table-column align="center" label="描述" prop="description" />
      <el-table-column align="center" label="Group_ID" prop="id" />
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="updateForm('update', scope.row)" v-hasPermi="['system:project:group:update']"> 编辑 </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['system:project:group:delete']"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表单弹窗：添加/修改 -->
    <UserForm ref="formRef" @success="handleSuccess" />
    <UserChildForm ref="formChildRef" @success="handleChildSuccess" />
  </ContentWrap>
</template>
<script lang="ts" setup>
import * as ProjectApi from '@/api/project'
import UserForm from './UserForm.vue'
import UserChildForm from './UserChildForm.vue'
defineOptions({ name: 'Store' })
const childBorder = ref(false)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { currentRoute } = useRouter() // 路由对象
const { query } = unref(currentRoute)
const loading = ref(false) // 列表的加载中
const list = ref([] as any) // 列表的数据
const queryParams = reactive({
  projectId: query.projectId
})
const queryFormRef = ref() // 搜索的表单
const expandKeys = ref([] as any) //展开行key
const currentRow = ref() // 新增成员行

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectApi.getProjectGroupList(queryParams)
    list.value = data.list
  } finally {
    loading.value = false
  }
}

// 展开行
const onExpand = (row, expandedRows) => {
  // 判断当前展开行
  if (expandedRows.find((ele) => ele == row)) {
    loading.value = true
    addExpandId(row.id)
    ProjectApi.getProjectGroupMember({ groupId: row.id, projectId: query.projectId })
      .then((res) => {
        row.children = res
      })
      .finally(() => {
        loading.value = false
      })
  }
}

const addExpandId = (id)=>{
  const result = expandKeys.value.find(i=>i == id)
  if(!result){
    expandKeys.value.push(id)
  }
}

/** 添加修改操作 */
const formRef = ref()
const updateForm = (type: string, row?: object) => {
  formRef.value.open(type, row)
}

// 删除
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProjectApi.deleteProjectGroup({ id, projectId: query.projectId })
    message.success(t('common.delSuccess'))
    // 刷新列表
    await handleSuccess()
  } catch {}
}

// 删除成员
const handleDeleteChildren = async (id, group) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProjectApi.deleteProjectGroupMember({ id, projectId: query.projectId, groupId:group.id })
    message.success(t('common.delSuccess'))
    // 刷新列表
    await handleChildDeleteSuccess(group)
  } catch {}
}

// 新增用户组成员
const formChildRef = ref()
const addChildForm = (type: string, row?: object) => {
  currentRow.value = row
  formChildRef.value.open(type, row)
}

//新增用户组成功
const handleSuccess = () => {
  expandKeys.value = []
  getList()
}

// 新增用户组成员成功
const handleChildSuccess = () => {
  ProjectApi.getProjectGroupMember({
    groupId: currentRow.value.id,
    projectId: query.projectId
  }).then((res) => {
    currentRow.value.children = res
  })
}
//删除成员成功
const handleChildDeleteSuccess = (row) => {
  currentRow.value = row
  ProjectApi.getProjectGroupMember({
    groupId: row.id,
    projectId: query.projectId
  }).then((res) => {
    currentRow.value.children = res
  })
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped lang="scss">
.child-btn {
  margin-left: 10px;
}
</style>
